package com.zprmb.udzt;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.adapter.DexteritRecordAdapter;
import com.zprmb.udzt.adapter.Ex_adapter;
import com.zprmb.udzt.bean.ExBean;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.ToJSON;
import com.zprmb.udzt.util.VolleryUtil;

public class MyExperienceGold extends BaseActivity implements OnClickListener{
	private PullToRefreshListView lv;
	private Button btn_out;
	private Ex_adapter adapter;
	private RelativeLayout rl_info;
	private DexteritRecordAdapter adapter2;
	private TextView tv_amount;
	private TimerTask timerTask;
    private TextView tv_total_shouyi,tv_yesterday,tv_rate,tv_total_amount;
	DecimalFormat df = new DecimalFormat("###0.000000");
	DecimalFormat df2 = new DecimalFormat("###0.00");
    private RelativeLayout rl_back;
    private ImageView img_nowork,img_nowork2;
	private Timer timer;
	private ImageView img_info;
	private int flag=0;
	private LinearLayout li_all;
	private LinearLayout li_info;
	double z=0,rate;
	double x=0;
	private int i;
	private ArrayList<Double> list=new ArrayList<Double>();
	private List<ExBean> lists=new ArrayList<ExBean>();
	private List<ExBean> list2=new ArrayList<ExBean>();
	private ImageView iv_v4_nonetwork;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.my_ex_gold2);
		flag=getIntent().getIntExtra("flag", 0);
		
		initView();
		li_all.setVisibility(View.GONE);
		initData(true,false);
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData(false,false);
	}
	private void initView() {
		// TODO Auto-generated method stub
		tv_amount=(TextView)findViewById(R.id.amount);
		lv=(PullToRefreshListView)findViewById(R.id.lv);
		btn_out=(Button)findViewById(R.id.btn_out);
		rl_info=(RelativeLayout)findViewById(R.id.rl_info);
		tv_rate=(TextView)findViewById(R.id.yuqi);
		li_info=(LinearLayout)findViewById(R.id.li_info);
		img_info=(ImageView)findViewById(R.id.img_info);
		tv_total_amount=(TextView)findViewById(R.id.tv_ex_amount);
		tv_yesterday=(TextView)findViewById(R.id.time);
		tv_total_shouyi=(TextView)findViewById(R.id.total_shouyi);
        li_all=(LinearLayout)findViewById(R.id.li_all);
        img_nowork=(ImageView)findViewById(R.id.iv_v4_nonetwork);
        img_nowork2=(ImageView)findViewById(R.id.iv_v4_nonetwork2);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		iv_v4_nonetwork=(ImageView)findViewById(R.id.iv_v4_nonetwork);
		img_nowork2.setOnClickListener(this);
		iv_v4_nonetwork.setOnClickListener(this);
		li_info.setOnClickListener(this);
		img_info.setOnClickListener(this);
		img_nowork.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		rl_info.setOnClickListener(this);
		btn_out.setOnClickListener(this);
	}
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			z=z+(x*rate/100/365/24/3600/60);

			
			tv_amount.setText(df.format(z)+"");
			if(msg.what==5){
				initData(true,true);
				Log.e("msg", msg.what+"");
			}
			
		};
	};
	private void initData(boolean isshow,final boolean flag) {
		// TODO Auto-generated method stub
	
			VolleryUtil volleryUtil=new VolleryUtil() {
				
				@Override
				public void onUpDate(JSONObject jsonObject) {
					// TODO Auto-generated method stub
					Log.e("ex", jsonObject.toString());
					if(jsonObject.optInt("success")==1){
						li_all.setVisibility(View.VISIBLE);
						img_nowork.setVisibility(View.GONE);
						JSONObject json1=jsonObject.optJSONObject("data");
						Gson gson=new Gson();
						list=gson.fromJson(json1.optString("numberArray"), new TypeToken<ArrayList<Double>>() {
						}.getType());
						lists=gson.fromJson(json1.optString("ueList"), new TypeToken<ArrayList<ExBean>>() {
						}.getType());
						
						adapter=new Ex_adapter(MyExperienceGold.this, lists);
						
						lv.setAdapter(adapter);
						if(lists.size()<1){
							lv.setVisibility(View.GONE);
							iv_v4_nonetwork.setVisibility(View.VISIBLE);
						}else{
							lv.setVisibility(View.VISIBLE);
							iv_v4_nonetwork.setVisibility(View.GONE);
						}
						tv_amount.setText(json1.optString("expTotalCurrentAmount"));
						 z=json1.optDouble("expTotalCurrentAmount");
						i=json1.optInt("remainingSecondsOfCurrentDay")*60;
						
						Log.e("系统时间",json1.optInt("remainingSecondsOfCurrentDay")+"");
						 tv_total_amount.setText(df2.format(Double.parseDouble(json1.optString("experiencingAmount"))));
						 x=json1.optDouble("experiencingAmount");
						 rate=json1.optDouble("fixedRate");
						 tv_rate.setText(df2.format(Double.parseDouble(json1.optString("fixedRate")))+"%");
						 tv_total_shouyi.setText(df2.format(Double.parseDouble(json1.optString("totalInterest"))));
						 tv_yesterday.setText(df2.format(Double.parseDouble(json1.optString("expYesterDayInterest"))));
					     timerTask = new TimerTask() {

							@Override
							public void run() {
								if(i==0&&timer!=null){
									timer.cancel();
									timer = null;  
									handler.sendEmptyMessage(5);
									
								}else{
									
									Message msg = new Message();
									msg.what = 0;
									handler.sendMessage(msg);
									i--;
									Log.e("i", i+"");
								}
								
							}
						}; 
                        
                        	 if(x>0){
                        		 Log.e("i>0", i+"");
                        		 if(timer==null){
                        			 
                        			 timer = new Timer();
                        			 timer.schedule(timerTask, 0, 1000/60);
                        		 }
                        	 }
						}
//					adapter=new Ex_adapter(My_ex_gold2.this, lists);
//					lv.setAdapter(adapter);
					ToJSON.initData(jsonObject.toString());
					
				}
				
				@Override
				public void onError() {
					// TODO Auto-generated method stub
					li_all.setVisibility(View.GONE);
					img_nowork.setVisibility(View.VISIBLE);
				}
			};
			volleryUtil.setHeaders("token", getToken());
			volleryUtil.setRequestMethod(0);
			volleryUtil.setShow(isshow);
			volleryUtil.execute(MyExperienceGold.this, Constant.URL+"/api/v5/experience/getMyExperienceData", "");
			
		
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(timer!=null){
			
			timer.cancel();
			timer = null;  
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(flag==1){
			Intent intent=new Intent(MyExperienceGold.this,MainActivity.class);
			startActivity(intent);
			if(timer!=null){
				
				timer.cancel();
				timer = null;  
			}
		}else{
			if(timer!=null){
				
				timer.cancel();
				timer = null;  
			}
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent=null;
		switch(v.getId()){
		
		case R.id.btn_out:
			intent=new Intent(MyExperienceGold.this,ExperienceGoldOutActivity.class);
			startActivity(intent);
			if(timer!=null){
				
				timer.cancel();
				timer = null;  
			}
			break;
		case R.id.rl_info:
			intent=new Intent(MyExperienceGold.this,ExperienceGoldInfo.class);
			startActivity(intent);
			if(timer!=null){
				
				timer.cancel();
				timer = null;  
			}
			break;
		case R.id.rl_back:
			if(flag==1){
				intent=new Intent(MyExperienceGold.this,MainActivity.class);
				startActivity(intent);
				if(timer!=null){
					
					timer.cancel();
					timer = null;  
				}
			}else{
				
				finish();
				if(timer!=null){
					
					timer.cancel();
					timer = null;  
				}
			}
			break;
		case R.id.iv_v4_nonetwork:
			initData(true,false);
			if(timer!=null){
				
				timer.cancel();
				timer = null;  
			}
			break;
		case R.id.img_info:
			intent=new Intent(MyExperienceGold.this,V4WebViewActivity.class);
			intent.putExtra("title", "体验金说明");
			intent.putExtra("url", "/h5/expIntro?app=true");
			startActivity(intent);
			if(timer!=null){
				
				timer.cancel();
				timer = null;  
			}
			break;
		case R.id.li_info:
			intent=new Intent(MyExperienceGold.this,V4WebViewActivity.class);
			intent.putExtra("title", "体验金说明");
			intent.putExtra("url", "/h5/expIntro?app=true");
			startActivity(intent);
			if(timer!=null){
				
				timer.cancel();
				timer = null;  
			}
			break;
		case R.id.iv_v4_nonetwork2:
			initData(true, false);
			break;
		
			
			
		}
		
	}

}
